package com.uphone.magicleafteacher.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtils {
    public static String getTimeFormLong(long time) {
        if (time <= 0) {
            return "00:00:00";
        }
        int hour = (int) ((time / 1000) / 60 / 60);
        int secondnd = (int) ((time / 1000) % 3600 / 60);
        int million = (int) ((time / 1000) % 60);
        String h = hour >= 10 ? String.valueOf(hour) : "0" + String.valueOf(hour);
        String f = secondnd >= 10 ? String.valueOf(secondnd) : "0" + String.valueOf(secondnd);
        String m = million >= 10 ? String.valueOf(million) : "0" + String.valueOf(million);
        return h + ":" + f + ":" + m;
    }

    /**
     * weekday=1，当天是周日；weekday=2，当天是周一；...;weekday=7，当天是周六。
     *
     * @param pTime
     * @return
     * @throws Exception
     */
    public static int dayForWeek(String pTime) {
        int dayForWeek = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d");
            Calendar c = Calendar.getInstance();
            c.setTime(format.parse(pTime));
            dayForWeek = c.get(Calendar.DAY_OF_WEEK);
        } catch (Exception e) {

        }
        return dayForWeek;
    }

    public static int getMonthDays(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (year % 4 == 0) {
                    return 29;
                } else {
                    return 28;
                }
        }
        return 0;
    }

    public static String num(int n) {
        String uppercase = "千百亿千百十万千百十个";
        int nLength = (n + "").length();
        String newStr = "";
        if (uppercase.length() - nLength < 0) {
            return "数字过长";
        }
        uppercase = uppercase.substring(uppercase.length() - nLength);
        for (int i = 0; i < nLength; i++) {
            newStr += "零一二三四五六七八九".charAt(Integer.parseInt((n + "").substring(i, i + 1))) + uppercase.charAt(i);
        }
        newStr = newStr.substring(0, newStr.length() - 1);
        return newStr;
    }
}
